package controllers;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import utils.BeanUtilities;
import utils.DAOUser;

import models.BeanProfile;
import models.User;

/**
 * Servlet implementation class ProfileController
 */
public class ProfileController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProfileController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();

		String message = "";
		synchronized (session) {

			String user = (String) session.getAttribute("user");
			User currentUser = (User) session.getAttribute("currentUser");
			String currUsername = currentUser.getUser();

			if (request.getParameter("action") != null) {
				session.setAttribute("action", request.getParameter("action"));
			} else {
				if (session.getAttribute("action") == null)
					session.setAttribute("action", "view");
			}
			try {

				DAOUser daoUser = new DAOUser();

				if (!daoUser.isAdmin(user)
						&& !currentUser.getUser().equals(user))
					session.setAttribute("action", "view");

				String action = (String) session.getAttribute("action");

				if (action.equals("edit")) {
					if (request.getParameter("editProfileSubmit") != null) {

						BeanProfile profile = new BeanProfile();
						BeanUtilities.populateBean(profile, request);
						if (profile.isComplete()) {
							daoUser.editProfile(profile, currUsername);

							request.setAttribute("profileBean", profile);
							RequestDispatcher dispatcher = request
									.getRequestDispatcher("home.jsp");
							if (dispatcher != null)
								dispatcher.forward(request, response);

						} else {
							// Form not submitted correctly.
							request.setAttribute("profileBean", profile);
							RequestDispatcher dispatcher = request
									.getRequestDispatcher("home.jsp");
							if (dispatcher != null)
								dispatcher.forward(request, response);
						}
					} else {
						// Just editting:
						BeanProfile profileBean = daoUser
								.getProfile(currUsername);
						request.setAttribute("profileBean", profileBean);
						RequestDispatcher dispatcher;
						dispatcher = request
								.getRequestDispatcher("editprofile.jsp");

						if (dispatcher != null)
							dispatcher.forward(request, response);
					}
				} else {
					// Just viewing:
					BeanProfile profileBean = daoUser.getProfile(currUsername);
					request.setAttribute("profileBean", profileBean);

					request.setAttribute("message", message);
					
					RequestDispatcher dispatcher;
					dispatcher = request.getRequestDispatcher("profile.jsp");
					if (dispatcher != null)
						dispatcher.forward(request, response);
					
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				message += "Problema al acceder al perfil del usuario";
			}
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
		// TODO Auto-generated method stub
	}

}
